package com.hhs.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hhs.storage.bean.BaseUser;
import com.hhs.storage.bean.Img;
import com.hhs.storage.bean.ResponseInfo;
import com.hhs.storage.dao.ImgDao;
import com.hhs.storage.service.ImgService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhs.storage.util.FileUpload;
import com.hhs.storage.util.StringUtil;
import com.hhs.storage.util.SysUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hhs
 * @since 2022-12-01
 */
@Service
@Transactional
public class ImgServiceImpl extends ServiceImpl<ImgDao, Img> implements ImgService {
    @Value("${upload.folderName}")
    private String folderName;
    @Override
    public Map<String, Object> upload(String module, String mainId,  MultipartFile[] imgs) {
        if(imgs.length>0){
            for (MultipartFile img : imgs) {
                String filePath = FileUpload.writeUploadFile(img,folderName,module);
                if(filePath!=null){
                    String t=StringUtil.replace(filePath,"\\","/");
                    Img temp = new Img();
                    temp.setId(SysUtil.UUID());
                    temp.setCreateTime(LocalDateTime.now());
                    temp.setPhotoUrl(t);
                    temp.setModule(module);
                    temp.setMainId(mainId);
                    this.save(temp);
                }
            }

        }
        return ResponseInfo.success(null);
    }

    @Override
    public  List<Img> getByMainId(Img img) {
        if(!StringUtils.isEmpty(img.getMainId())){
            QueryWrapper ew = new QueryWrapper<Img>();
                ew.eq("main_id", img.getMainId());
            if(!StringUtils.isEmpty(img.getModule())){
                ew.eq("module", img.getModule());
            }
            ew.orderByAsc("create_time");
            List<Img> list = this.list(ew);
            return list;
        }
        return new ArrayList<>();
    }

    @Override
    public Map<String, Object> deleById(String id) {

        Img img = this.baseMapper.selectById(id);
        if(img!=null){
            this.baseMapper.deleteById(id);
            FileUpload.deleteFile(folderName,img.getPhotoUrl());
        }
        return ResponseInfo.success(null);
    }

    @Override
    public Map editorUpload(MultipartFile file) {
        //返回格式按照wangEditor的要求返回
        Map map = new HashMap();
        map.put("code",1);
        try {
            Map temp = new HashMap();
            String filePath = FileUpload.writeUploadFile(file,folderName,"editor");
            if(filePath!=null){
                //前后端分离，要返回后端的全部地址
                filePath=StringUtil.replace(filePath,"\\","/");
                String url=SysUtil.BASEURL+"upload"+filePath;
                temp.put("url",url);
            }
            temp.put("alt","");
            temp.put("href","");
            map.put("data",temp);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code",0);
            map.put("msg",e.getMessage());
        }
        return map;
    }
}
